//
//  LendingRateViewController.m
//  EMMApp
//
//  Created by Mac on 16/7/1.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "LendingRateViewController.h"

/** Cell
 *
 *  Cell导入头文件.h
 */
#import "MarketingCell.h"
#import "NewCustomersCell.h"
#import "ShoufuCell.h"
#import "LendingRateCell.h"
#import "LendingRateOneCell.h"

@interface LendingRateViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    NSMutableDictionary *dic;//存对应的数据
    NSMutableArray *selectedArr;//二级列表是否展开状态
    NSMutableArray *grouparr0;
    NSMutableArray *titleDataArray;
    NSMutableArray *grouparr1;
    NSString *xzIndex;
    NSMutableArray *grouparr3;
    NSMutableArray *grouparr4;
    NSMutableArray *grouparr5;
    NSString *_isFals;
}

/**
 *  创建 - UITableView
 */
@property (nonatomic, strong) UITableView *knowDtailTableView;

/**
 *  数组
 */
@property (nonatomic, strong) NSMutableArray *seleArr;

/**
 *  公积金输入框
 */
@property (nonatomic, strong) UITextField *gjjTextField;

/**
 *  字段 - 公积金利率
 */
@property (nonatomic, strong) NSString *gjjString;

/**
 *  字段 - 利率
 */
@property (nonatomic, strong) NSString *llString;   //利率

/**
 *  字段 - 倍数
 */
@property (nonatomic, strong) NSString *beishuString;//倍数

/**
 *  字段 - 商业利率
 */
@property (nonatomic, strong) NSString *zongshuString;

@end

@implementation LendingRateViewController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavigationController];
    self.view.backgroundColor = [UIColor whiteColor];
    dic = [[NSMutableDictionary alloc] init];
    selectedArr = [[NSMutableArray alloc] initWithObjects:@"0",nil];
    
    _knowDtailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight-60) style:UITableViewStylePlain];
    _knowDtailTableView.delegate = self;
    _knowDtailTableView.dataSource = self;
    _knowDtailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _knowDtailTableView.backgroundColor = [UIColor whiteColor];
    _knowDtailTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_knowDtailTableView];
    [self initDataSource];
    [self creatDoneShowUI];
    _beishuString = @"1.00";
    _llString = @"5.15";
    _zongshuString = @"5.15";
    _gjjString = @"3.25";
    xzIndex = @"3";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 导航创建
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"贷款利率";
    [self configLeftBtn];
}

#pragma mark - 控件创建初始化
- (void)creatDoneShowUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT-SafeAreaTopHeight-60, EMM_WIDTH, 60)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    doneBtn.frame = CGRectMake(20, 5, EMM_WIDTH-40, 50);
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    doneBtn.backgroundColor = [UIColor colorFromHexCode:@"#F87144"];
    [doneBtn addTarget:self action:@selector(doneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    doneBtn.layer.masksToBounds = YES;
    doneBtn.layer.cornerRadius = 5;
    [view addSubview:doneBtn];
}

#pragma mark - 确定
- (void)doneBtnClick:(UIButton *)btn {
    NSString *str1 = nil;
    NSString *str2 = nil;
    NSString *str3 = nil;
    
    if ([_isFals isEqualToString:@"0"]) {
        str1 = @"自定义";
        str2 = _zongshuString;
        str3 = _gjjString;
    }else {
        str1 = [grouparr1 objectAtIndex:[xzIndex integerValue]];
        str2 = _zongshuString;
        str3 = _gjjString;
    }
    if ([_isTpye isEqualToString:@"0"]) {
        if (_llString.length == 0) {
            [self showToast:@"商业利率未输入或者未选择"];
            return;
        }
        
        if (_beishuString.length == 0) {
            [self showToast:@"倍数未输入或者未选择"];
            return;
        }
        
        if (_gjjString.length == 0) {
            [self showToast:@"公积金利率未输入或者未选择"];
            return;
        }
    }else if ([_isTpye isEqualToString:@"1"]) {
        if (_gjjString.length == 0) {
            [self showToast:@"公积金利率未输入或者未选择"];
            return;
        }
    }else if ([_isTpye isEqualToString:@"2"]) {
        if (_llString.length == 0) {
            [self showToast:@"商业利率未输入或者未选择"];
            return;
        }
        
        if (_beishuString.length == 0) {
            [self showToast:@"倍数未输入或者未选择"];
            return;
        }
    }
    if (_lendingRateBlock) {
        _lendingRateBlock(str1,str2,str3,_beishuString);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - post
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - 初始化数据
-(void)initDataSource {
    if ([_isTpye isEqualToString:@"0"]) {
        grouparr0 = [[NSMutableArray alloc] initWithObjects:@"商贷利率",@"公积金利率",@"当前基准利率:", nil];
    }else if ([_isTpye isEqualToString:@"1"]) {
        grouparr0 = [[NSMutableArray alloc] initWithObjects:@"公积金利率", nil];
    }else if ([_isTpye isEqualToString:@"2"]) {
        grouparr0 = [[NSMutableArray alloc] initWithObjects:@"商贷利率",@"当前基准利率:", nil];
    }
    grouparr1 = [[NSMutableArray alloc] initWithObjects:
                 @"15年8月26利率上限(1.1倍)",@"15年8月26利率下限(85折)",
                 @"15年8月26利率下限(7折)",@"15年8月26基准利率",
                 @"15年6月28利率上限(1.1倍)",@"15年6月28利率下限(85折)",
                 @"15年6月28利率下限(7折)",@" 15年6月28基准利率",
                 @"15年5月11利率上限(1.1倍)",@"15年5月11利率上下限(85折)",
                 @"15年5月11利率下限(7折)",@"15年5月11基准利率",
                 @"15年3月1利率上限(1.1倍)",@"15年3月1利率上下限(85折)",
                 @"15年3月1利率下限(7折)",@"15年3月1基准利率",nil];
    
     grouparr3 = [[NSMutableArray alloc] initWithObjects:
                  @"5.15",@"5.15",
                  @"5.15",@"5.15",
                  @"5.40",@"5.40",
                  @"5.40",@"5.40",
                  @"5.56",@"5.56",
                  @"5.56",@"5.56",
                  @"5.90",@"5.90",
                  @"5.90",@"5.90",nil];
     grouparr4 = [[NSMutableArray alloc] initWithObjects:
                  @"3.25",@"3.25",
                  @"3.25",@"3.25",
                  @"3.50",@"3.50",
                  @"3.50",@"3.50",
                  @"3.75",@"3.75",
                  @"3.75",@"3.75",
                  @"4.00",@"4.00",
                  @"4.00",@"4.00",nil];
    grouparr5 = [[NSMutableArray alloc] initWithObjects:
                 @"1.10",@"0.85",
                 @"0.70",@"1.00",
                 @"1.10",@"0.85",
                 @"0.70",@"1.00",
                 @"1.10",@"0.85",
                 @"0.70",@"1.00",
                 @"1.10",@"0.85",
                 @"0.70",@"1.00", nil];
}

#pragma mark - UITableView代理
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *string = [NSString stringWithFormat:@"%ld",section];
    if ([string isEqualToString:@"0"]) {
        return grouparr0.count;
    }else {
        return grouparr1.count;
    }
    return 0;
}

//设置表头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 50;
}

//设置view，将替代titleForHeaderInSection方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, tableView.frame.size.width-20, 20)];
    titleLabel.text = @"或选择常用比例";
    [view addSubview:titleLabel];
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49.5, EMM_WIDTH, 0.5)];
    lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, EMM_WIDTH, 50);
    button.tag = 100+section;
    //    [button addTarget:self action:@selector(doButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    view.backgroundColor = [UIColor colorFromHexCode:@"#fff5e9"];
    UILabel *sLabel6 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 4, 30)];
    sLabel6.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    [view addSubview:sLabel6];
    if (section == 0) {
        return nil;
    }
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    static NSString *CellIdentifier = @"MarketingCell";
    MarketingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MarketingCell" owner:self options:nil] lastObject];
    }
    if ([indexStr isEqualToString:@"0"]) {
        static NSString *CellIdentifier = @"NewCustomersCell";
        NewCustomersCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"NewCustomersCell" owner:self options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLbael.text = [grouparr0 objectAtIndex:indexPath.row];
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 49.5, EMM_WIDTH-20, 0.5)];
        lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
        [cell addSubview:lineImage];
        if ([_isTpye isEqualToString:@"0"]) {
            if (indexPath.row == 0) {
                cell.nameLbael.hidden = YES;
                static NSString *CellIdentifier = @"LendingRateCellID";
                LendingRateCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"LendingRateCell" owner:self options:nil] lastObject];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 49.5, EMM_WIDTH-30, 0.5)];
                lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
                [cell addSubview:lineImage];
                if ([NSString isStringEmpty2:_llString] == NO) {
                    cell.llTextField.text = _llString;
                }else {
                    cell.llTextField.placeholder = @"输入";
                }
                cell.llTextField.keyboardType = UIKeyboardTypeDecimalPad;
                cell.llTextField.delegate = self;

                if ([NSString isStringEmpty2:_beishuString] == NO) {
                    cell.beishuTextField.text = _beishuString;
                }else {
                    cell.beishuTextField.placeholder = @"输入";
                }
                cell.beishuTextField.keyboardType = UIKeyboardTypeDecimalPad;
                cell.beishuTextField.delegate = self;
                
                if ([NSString isStringEmpty2:_zongshuString] == NO) {
                    cell.jieguoLabel.text = _zongshuString;
                }else {
                    cell.jieguoLabel.text = nil;
                }
                return  cell;
            }else if (indexPath.row == 1) {
                cell.bitianLabel.hidden = YES;
                cell.customLabel.hidden = YES;
                cell.qjImageView.hidden = YES;
                cell.custTextField.hidden = YES;
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH-30-15, 10, 30, 30)];
                label.textAlignment = NSTextAlignmentRight;
                label.text = @"%";
                label.font = [UIFont systemFontOfSize:14];
                [cell addSubview:label];
                
                _gjjTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-50, 10, 200, 30)];
                _gjjTextField.delegate = self;
                _gjjTextField.returnKeyType =UIReturnKeyDone;
                _gjjTextField.textAlignment = NSTextAlignmentRight;
                _gjjTextField.font = [UIFont systemFontOfSize:15];
                _gjjTextField.keyboardType = UIKeyboardTypeDecimalPad;
                [cell addSubview:_gjjTextField];
                if ([NSString isStringEmpty2:_gjjString] == YES) {
                    _gjjTextField.placeholder = @"请输入";
                }else {
                    _gjjTextField.text = _gjjString;
                }
            }else if (indexPath.row == 2) {
                static NSString *CellIdentifier = @"LendingRateOneCellId";
                LendingRateOneCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"LendingRateOneCell" owner:self options:nil] lastObject];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 59, EMM_WIDTH-30, 1)];
                lineImage.backgroundColor = [UIColor colorFromHexCode:@"f0f0f0"];
                cell.lendView.layer.borderWidth = 1.0;
                cell.lendView.layer.borderColor = [[UIColor colorFromHexCode:@"#E4E4E4"] CGColor];
                cell.lendView.backgroundColor = [UIColor colorFromHexCode:@"#FFF5E9"];
                cell.syBeifenLabel.text = @"5.15%";
                cell.gjjBaifenLabel.text = @"3.25%";
                cell.gjjLabel.text = @",公积金";
                return cell;
            }
        }else if ([_isTpye isEqualToString:@"1"]) {
            if (indexPath.row == 0) {
                cell.bitianLabel.hidden = YES;
                cell.customLabel.hidden = YES;
                cell.qjImageView.hidden = YES;
                cell.custTextField.hidden = YES;
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH-30-15, 10, 30, 30)];
                label.textAlignment = NSTextAlignmentRight;
                label.text = @"%";
                label.font = [UIFont systemFontOfSize:14];
                [cell addSubview:label];
                
                _gjjTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-50, 10, 200, 30)];
                _gjjTextField.delegate = self;
                _gjjTextField.returnKeyType =UIReturnKeyDone;
                _gjjTextField.textAlignment = NSTextAlignmentRight;
                _gjjTextField.font = [UIFont systemFontOfSize:15];
                _gjjTextField.keyboardType = UIKeyboardTypeDecimalPad;
                [cell addSubview:_gjjTextField];
                if ([NSString isStringEmpty2:_gjjString] == YES) {
                    _gjjTextField.placeholder = @"请输入";
                }else {
                    _gjjTextField.text = _gjjString;
                }
            }
        }else if ([_isTpye isEqualToString:@"2"]) {
            if (indexPath.row == 0) {
                static NSString *CellIdentifier = @"LendingRateCellID";
                LendingRateCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"LendingRateCell" owner:self options:nil] lastObject];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 49.5, EMM_WIDTH-30, 0.5)];
                lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
                [cell addSubview:lineImage];
                if ([NSString isStringEmpty2:_llString] == NO) {
                    cell.llTextField.text = _llString;
                }else {
                    cell.llTextField.placeholder = @"输入";
                }
                cell.llTextField.keyboardType = UIKeyboardTypeDecimalPad;
                cell.llTextField.delegate = self;

                if ([NSString isStringEmpty2:_beishuString] == NO) {
                    cell.beishuTextField.text = _beishuString;
                }else {
                    cell.beishuTextField.placeholder = @"输入";
                }
                cell.beishuTextField.keyboardType = UIKeyboardTypeDecimalPad;
                cell.beishuTextField.delegate = self;
                
                if ([NSString isStringEmpty2:_zongshuString] == NO) {
                    cell.jieguoLabel.text = _zongshuString;
                }else {
                    cell.jieguoLabel.text = nil;
                }
                return  cell;
            }else {
                static NSString *CellIdentifier = @"LendingRateOneCellId";
                LendingRateOneCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"LendingRateOneCell" owner:self options:nil] lastObject];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 59, EMM_WIDTH-30, 1)];
                lineImage.backgroundColor = [UIColor colorFromHexCode:@"f0f0f0"];
                cell.lendView.layer.borderWidth = 1.0;
                cell.lendView.layer.borderColor = [[UIColor colorFromHexCode:@"#E4E4E4"] CGColor];
                cell.lendView.backgroundColor = [UIColor colorFromHexCode:@"#FFF5E9"];
                cell.syBeifenLabel.text = @"5.15%";
                cell.gjjBaifenLabel.hidden = YES;
                cell.gjjLabel.hidden = YES;
                return cell;
            }
        }
        
        return cell;
    }else {
        static NSString *CellIdentifier = @"ShoufuCell";
        ShoufuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ShoufuCell" owner:self options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 49.5, EMM_WIDTH-20, 0.5)];
        lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
        [cell addSubview:lineImage];
        cell.nameLabel.text = [grouparr1 objectAtIndex:indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld",indexPath.row];
        if ([string isEqualToString:xzIndex]) {
            cell.xzImageView.image = [UIImage imageNamed:@"choose"];
        }else {
            cell.xzImageView.image = [UIImage imageNamed:@"choose_no"];
        }
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    if ([indexStr isEqualToString:@"0"]) {
    }else {
        xzIndex = [NSString stringWithFormat:@"%ld",indexPath.row];
        [_knowDtailTableView reloadData];
        _llString = [grouparr3 objectAtIndex:indexPath.row];
        _beishuString = [grouparr5 objectAtIndex:indexPath.row];
        _zongshuString = [NSString stringWithFormat:@"%.2f",[_llString floatValue] * [_beishuString floatValue]];
        NSString *gjjS = [grouparr4 objectAtIndex:indexPath.row];
        _gjjString = [NSString stringWithFormat:@"%.2f",[gjjS floatValue] * [_beishuString floatValue]];
        _isFals = @"1";
    }
}

#pragma mark - 二级展开按钮触发方法
-(void)doButton:(UIButton *)sender {
    NSString *string = [NSString stringWithFormat:@"%ld",sender.tag-100];
    //数组selectedArr里面存的数据和表头想对应，方便以后做比较
    if ([selectedArr containsObject:string]) {
        [selectedArr removeObject:string];
    } else  {
        [selectedArr addObject:string];
    }
    [_knowDtailTableView reloadData];
}

#pragma mark - UITextField代理方法
-(void)textFieldDidEndEditing:(UITextField *)textField {
    LendingRateCell *cell = (LendingRateCell*)[_knowDtailTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    if (textField == _gjjTextField) {
        _isFals = @"0";
        if (textField.text.length > 6) {
            [self showToast:@"输入不能超过5位,重新输入"];
            [_knowDtailTableView reloadData];
            return;
        }else {
            _gjjString = textField.text;
        }
        float s = [_gjjString floatValue];
        if (s <= 100.00) {
            
        }else {
            [self showToast:@"公积金利率不能大于100"];
            _gjjString = @"";
        }
    }else if (textField == cell.llTextField) {
        _isFals = @"0";
        if (textField.text.length > 6) {
            [self showToast:@"输入不能超过5位,重新输入"];
            [_knowDtailTableView reloadData];
            return;
        }else {
            _llString = textField.text;
        }
        if ([NSString isStringEmpty2:_beishuString] == NO) {
            _zongshuString = [NSString stringWithFormat:@"%.2f",[_llString floatValue] * [_beishuString floatValue]];
        }
    }else if (textField == cell.beishuTextField) {
        _isFals = @"0";
        if (textField.text.length > 6) {
            [self showToast:@"输入不能超过5位,重新输入"];
            [_knowDtailTableView reloadData];
            return;
        }else {
            _beishuString = textField.text;
        }
        if ([NSString isStringEmpty2:_llString] == NO) {
            _zongshuString = [NSString stringWithFormat:@"%.2f",[_llString floatValue] * [_beishuString floatValue]];
        }
    }
    [_knowDtailTableView reloadData];
}

@end
